﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MySkiPatrolSite.Controllers
{
    public class AdminController : Controller
    {
        AdminSvc.AdminClient svc = new AdminSvc.AdminClient();

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            
            return View();
        }

        public ActionResult Members()
        {
            if ((Session == null) || Session["MEMBER"] == null || (!Security.IsLoggedIn((MySkiPatrolSite.MembershipSvc.Member)Session["MEMBER"])))
            {
                //not logged in
                return RedirectToAction("LogOn", "Account");
            }

            if (!Security.IsInGroup("SITEADMIN",(MySkiPatrolSite.MembershipSvc.Member)Session["MEMBER"]))
            {
                //not authorized
                TempData["ALERT"] = "You have tried to access a section of the site you requested data are not authorized for.";
                return RedirectToAction("NotAuthorized", "Home");
            }


            AdminSvc.Patrol model = new AdminSvc.Patrol();
            model.DataTransaction = new AdminSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;
            model.ID = ((AdminSvc.Patrol)Session["PATROL"]).ID;

            model = svc.GetMembers(model);
            

            return View(model);
        }

        public ActionResult MemberDetails(string id)
        {

            AdminSvc.Member model = new AdminSvc.Member();
            model.DataTransaction = new AdminSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;
            model.ID = id;

            if (model.IDCards == null) model.IDCards = new List<AdminSvc.IDCard>().ToArray();

            model = svc.GetMember(model);


            return View(model);
        }

        [HttpPost]
        public ActionResult MemberDetails(AdminSvc.Member model, FormCollection form)
        {
            

            model.DataTransaction = new AdminSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;


            //model = svc.GetMember(model);

            switch (form.GetValue("action").AttemptedValue.ToUpper())
            {
                case "DELETE":
                    model.DataTransaction.Action = AdminSvc.DataAction.Delete;
                    model.DataTransaction.ProcessDateTime = DateTimeOffset.Now;
                    svc.PutMember(model);
                    break;
                case "SAVE":
                    if (model.ID.ToUpper() == "NEW")
                    {
                        model.ID = "";
                        model.DataTransaction.Action = AdminSvc.DataAction.Add;
                    }
                    else
                    {
                        model.DataTransaction.Action = AdminSvc.DataAction.Update;
                    }
                    svc.PutMember(model);
                    break;
                case "CANCEL":
                    return RedirectToAction("Members", "Admin");
                    break;
                default:
                    return View(model);
                    break;
            }


            return RedirectToAction("Members", "Admin");
        }

        public ActionResult MemberDelete(string id)
        {

            AdminSvc.Member model = new AdminSvc.Member();
            model.DataTransaction = new AdminSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;
            model.ID = id;
            model.DateDeleted = DateTimeOffset.Now;
            model.DataTransaction.Action = AdminSvc.DataAction.Delete;

            model = svc.PutMember(model);

            return RedirectToAction("Members", "Admin");
        }

        [HttpPost]
        public ActionResult AddMemberIDCard(string id, string memberid, string expiredate)
        {
            AdminSvc.IDCard model = new AdminSvc.IDCard();
            model.DataTransaction = new AdminSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;
            model.CardID = id;
            model.MemberID = memberid;
            model.Expires = DateTimeOffset.Parse(expiredate);
            model.DataTransaction.Action = AdminSvc.DataAction.Add;

            if (model.MemberID.ToLower() != "new")
            {
                //if this is not a NEW member, go ahead and submit...
                model = svc.PutIDCard(model);
            }
            else
            {
                return Json(model);
            }

            return Json(model);
            //return Json(new { msg = model.DataTransaction.Success });
        }

        [HttpPost]
        public ActionResult MemberNew(AdminSvc.Member model, FormCollection form)
        {
            model.DataTransaction = new AdminSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;


            //model = svc.GetMember(model);

            switch (form.GetValue("action").AttemptedValue.ToUpper())
            {
                case "SAVE":
                    model.DataTransaction.Action = AdminSvc.DataAction.Add;
                    svc.PutMember(model);

                    //need to determine the expiration date for the ID Card

                    break;
                case "CANCEL":
                    return RedirectToAction("Members", "Admin");
                    break;
                default:
                    return View(model);
                    break;
            }


            return View(model);
        }

        public ActionResult MemberNew()
        {
            AdminSvc.Member model = new AdminSvc.Member();
            model.ID = "0";

            return View(model);
        }

        public ActionResult DeleteMemberIdCard(string id)
        {

            AdminSvc.IDCard model = new AdminSvc.IDCard();
            model.DataTransaction = new AdminSvc.DataTransaction();
            model.DataTransaction.PatrolPrefix = ((AdminSvc.Patrol)Session["PATROL"]).dbPrefix;
            model.CardID = id;
            model.DateDeleted = DateTimeOffset.Now;
            model.DataTransaction.Action = AdminSvc.DataAction.Delete;

            model = svc.PutIDCard(model);

            if (model.DataTransaction.Success)
            {
                return RedirectToAction("MemberDetails", "Admin", new { id = model.MemberID });
            }

            return RedirectToAction("Members", "Admin");
        }

    }
}
